from django.shortcuts import render, HttpResponse, redirect, reverse
from django.views.decorators.http import require_POST, require_GET
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required

from xadmin.models import PartElement, Part
from xadmin.forms import PartElementForm
from xadmin.plugins.auditlog import add_log


@login_required
def index(request):
    siteid = request.GET.get('siteid', '1')
    partid = request.GET.get('partid')
    part = Part.objects.get(id=partid)
    ele_list = PartElement.objects.filter(part_id=partid)
    mbx = ('部件管理', part.name)
    context = {
        'siteid': siteid,
        'mbx': mbx,
        'ele_list': ele_list,
        'partid': partid
    }
    return render(request, 'xadmin/partElement/list.html', context)


@login_required
def add(request):
    siteid = request.GET.get('siteid', '1')
    partid = request.GET.get('partid')
    part = Part.objects.get(id=partid)

    context = {
        'siteid': siteid,
        'partid': partid,
    }
    if request.method == 'GET':
        ele_form = PartElementForm.PartElementForm()
        context['mbx'] = ('部件管理', part.name, '添加元素')
        context['ele_form'] = ele_form
        return render(request, 'xadmin/partElement/add.html', context)
    else:
        ele_form = PartElementForm.PartElementForm(request.POST, request.FILES)
        if ele_form.is_valid():
            new_ele = ele_form.save(commit=False)
            new_ele.part_id = partid
            new_ele.save()
            action = '为部件《%s》添加了元素《%s》' % (new_ele.part.name, new_ele.name)
            add_log(request, action, siteid)
            return redirect(reverse('elelist') + '?partid=' + partid + '&siteid=' + siteid)
        else:
            error = ele_form.errors
            context['error'] = error
            context['ele_form'] = ele_form
            return render(request, 'xadmin/partElement/add.html', context)


@login_required
def edit(request):
    siteid = request.GET.get('siteid', '1')
    eleid = request.GET.get('eleid')
    ele_obj = PartElement.objects.get(id=eleid)
    part_obj = Part.objects.get(id=ele_obj.part_id)
    context = {
        'siteid': siteid,
        'eleid': eleid,
        'partid': part_obj.id,
        'mbx': ('部件管理', part_obj.name, '编辑元素')
    }

    if request.method == 'GET':
        ele_form = PartElementForm.PartElementForm(instance=ele_obj)
        context['ele_form'] = ele_form
        return render(request, 'xadmin/partElement/add.html', context)
    else:
        ele_form = PartElementForm.PartElementForm(request.POST, request.FILES, instance=ele_obj)
        if ele_form.is_valid():
            ele_form.save()
            action = '编辑了部件元素《%s》' % (ele_form.cleaned_data['name'])
            add_log(request, action, siteid)
            return redirect(reverse('elelist') + '?partid=' + str(part_obj.id) + '&siteid=' + siteid)
        else:
            error = ele_form.errors
            context['error'] = error
            context['ele_form'] = ele_form
            return render(request, 'xadmin/partElement/add.html', context)


@login_required
@require_POST
def ele_del(request):
    eleid = request.POST.get('eleid')
    try:
        ele_obj = PartElement.objects.get(id=eleid)
        part_obj = Part.objects.get(id=ele_obj.part_id)
        ele_obj.delete()
        message = {
            'status': '200',
            'message': '删除成功'
        }
        action = '删除了部件《%s》的元素《%s》' % (part_obj.name, ele_obj.name)
        add_log(request, action, part_obj.site_id)
    except:
        message = {
            'status': '500',
            'message': '删除失败'
        }
    return JsonResponse(message)
